package c04.reentrantlock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

import static util.Sleeper.sleep;

@Slf4j(topic = "c.ReentrantLockInterrupt")
public class ReentrantLockInterrupt {
    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            log.debug("t1 尝试获取锁");
            try {
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.debug("t1 没有获取到锁");
                return;
            }

            log.debug("t1 获取锁");
            lock.unlock();
        }, "t1");

        lock.lock();
        t1.start();

        sleep(1);
        log.debug("中断线程 t1");
        t1.interrupt();
    }
}
